#include"SL.h"
//void Print(SL*ps)
//{
  //  for(int i=0;i<ps->size;i++)
   // {
     //   printf("%d ",ps->p[i]);
   // }
   // printf("\n");
//}
void SLInit(SL* ps)
{
    ps->p=NULL;
    ps->capacity=ps->size=0;
}
void SLStory(SL* ps)
{
    if(ps->p!=NULL)
    {
        free(ps->p);
        ps->p=NULL;
    }
}
void SLPushBack(SL* ps, type x)
{
    assert(ps);
    if(ps->capacity==ps->size)
    {
        if(ps->capacity==0)
        {
        ps->capacity=4;
    }else
    {
        ps->capacity=ps->capacity*2;
    }
    }
    type* temp=realloc(ps->p,ps->capacity*sizeof(type));
    if(temp==NULL)
    {
        exit(1);
    }else
    {
        ps->p=temp;
    }
    ps->p[ps->size]=x;
    ps->size++;
}
void SLPushFront(SL* ps, type x)
{
    assert(ps);
     if(ps->capacity==ps->size)
    {
        if(ps->capacity==0)
        {
        ps->capacity=4;
    }else
    {
        ps->capacity=ps->capacity*2;
    }
    }
    type* temp=realloc(ps->p,ps->capacity*sizeof(type));
    if(temp==NULL)
    {
        exit(1);
    }else
    {
        ps->p=temp;
    }
    for(int i=ps->size;i>0;i--)
    {
        ps->p[i]=ps->p[i-1];
    }
    ps->p[0]=x;
    ps->size++;
}
void SLPopBack(SL* ps)
{
    assert(ps);
    if(ps->size==0)
    {
        exit(1);
    }
    ps->size--;
}
void SLPopFront(SL* ps)
{
    assert(ps);
    if(ps->size==0)
    {
        exit(1);
    }
    for(int i=0;i<ps->size-1;i++)
    {
        ps->p[i]=ps->p[i+1];
    }
    ps->size--;
}
void SLInsert(SL* ps, int pos, type x)
{
    assert(ps);
    assert(pos>=0&&pos<=ps->size);
    if(ps->capacity==ps->size)
    {
        if(ps->capacity==0)
        {
        ps->capacity=4;
    }else
    {
        ps->capacity=ps->capacity*2;
    }
    }
    type* temp=realloc(ps->p,ps->capacity*sizeof(type));
    if(temp==NULL)
    {
        exit(1);
    }else
    {
        ps->p=temp;
    }
    for(int i=ps->size;i>pos;i--)
    {
        ps->p[i]=ps->p[i-1];
    }
    ps->p[pos]=x;
    ps->size++;
}
void SLErase(SL* ps, int pos)
{
    assert(ps);
    assert(pos>=0&&pos<ps->size);
    for(int i=pos;i<ps->size-1;i++)
    {
        ps->p[i]=ps->p[i+1];
    }
    ps->size--;
}
//#define action
#ifdef action
void SLTest01()
 {
    SL sl;
     SLInit(&sl);
     SLPushBack(&sl, 1);
     SLPushBack(&sl,  2);
     SLPushBack(&sl, 3);
     SLPushBack(&sl,  4);
     Print(&sl);
     SLPopBack(&sl);
     Print(&sl);
      SLPushFront(&sl, 5);
      Print(&sl);
      SLPopFront(&sl);
      Print(&sl);

}
int main()
{
     SLTest01();
    return 0;
}
#endif